package com.ihr360.insurance.dao;

import com.ihr360.insurance.model.dto.ledger.MonthlyLedger;
import com.ihr360.rest.reposity.SimpleRestReposity;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import java.util.List;

@RepositoryRestResource
public interface MonthlyLedgerReposity extends SimpleRestReposity<MonthlyLedger, Long>,
        PagingAndSortingRepository<MonthlyLedger, Long>,
        JpaSpecificationExecutor<MonthlyLedger> {

    List<MonthlyLedger> findAllByCompanyIdAndUserIdAndLedgerYearAndLedgerMonth(String companyId,
                                                                               String userId,
                                                                               int ledgerYear,
                                                                               int ledgerMonth);

    @Query("select t.staff.id from MonthlyLedger t " +
            "where t.companyId=:companyId and t.userId=:userId " +
            "and t.ledgerYear=:ledgerYear and t.ledgerMonth=:ledgerMonth")
    List<String> findAllStaffIds(@Param("companyId") String companyId,
                                 @Param("userId") String userId,
                                 @Param("ledgerYear") int ledgerYear,
                                 @Param("ledgerMonth") int ledgerMonth);

    void deleteByCompanyIdAndUserIdAndLedgerYearAndLedgerMonth(String companyId,
                                                               String userId,
                                                               int ledgerYear,
                                                               int ledgerMonth);

    List<MonthlyLedger> findAllByCompanyIdAndUserIdAndLedgerYearAndLedgerMonthAndStaffIdIn(
            String companyId, String userId, int ledgerYear, int ledgerMonth, List<String> staffIds);

    int countByCompanyIdAndUserIdAndLedgerYearAndLedgerMonthAndSiCompanyBenefitIsNotNull(String companyId,
                                                                                         String userId,
                                                                                         int ledgerYear,
                                                                                         int ledgerMonth);

    int countByCompanyIdAndUserIdAndLedgerYearAndLedgerMonthAndHfCompanyBenefitIsNotNull(String companyId,
                                                                                         String userId,
                                                                                         int ledgerYear,
                                                                                         int ledgerMonth);
}